package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.apache.ibatis.cache.decorators.LruCache;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServicelmpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 根据id来查找套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO SelectById(Long id) {
       SetmealVO setmealVO =setmealMapper.SelectByid(id);
        List<SetmealDish> list=setmealDishMapper.SelectSetmealId(id);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void sava(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.sava(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealDishMapper.savaSetmealDish(setmealDishes);
        }

    }

    /**
     * 根据id来批量删除套餐
     * @param ids
     */
    @Override
    public void DeleteById(List<Long> ids) {
       // 在售状态下，不可删除套餐
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.SelectByid(id);
            if(setmealVO.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.SelectSetmealId(ids);
    }

    /**
     * 修改套餐状态
     * @param status
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modifyStatus(Integer status, Long id) {
        // 套餐内如果有停售菜品，则套餐无法上架
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        if (status == StatusConstant.ENABLE) {
            //如果是起售
            List<Dish> list = setmealDishMapper.SelectsetmealDish(id);
            list.forEach(dish -> {
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        setmealMapper.update(setmeal);

    }

    /**
     * 分页查询
     * @param queryDTO
     * @return
     */
    @Override
    public PageResult pagelist(SetmealPageQueryDTO queryDTO) {
        PageHelper.startPage(queryDTO.getPage(),queryDTO.getPageSize());
       List<SetmealVO> list =setmealMapper.pagelist(queryDTO);
        Page<SetmealVO> page = (Page<SetmealVO>) list;
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void modifysetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        /* 删除原有的套餐信息 */
        setmealDishMapper.delectSetmealId(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null || setmealDishes.size()>0) {
            setmealDishMapper.savaSetmealDish(setmealDishes);
        }
    }

    @Override
    public List<Setmeal> selectByCategoryId(Long categoryId) {
       List<Setmeal>setmeals =setmealMapper.selectByCategoryId(categoryId);
        return setmeals;
    }

    @Override
    public List<DishItemVO> SelectDish(Long id) {
      List<DishItemVO>lists  =setmealDishMapper.SelectDishItemVo(id);
        return lists;
    }

}
